import { get } from '@/utils/request';

export interface LoginLog {
  id: string;
  tenantId: string | null;
  userId: string | null;
  username: string;
  loginType: 'password' | 'sms' | 'oauth' | 'other';
  status: 'success' | 'failed';
  ip: string;
  ipLocation: string | null;
  userAgent: string | null;
  browser: string | null;
  os: string | null;
  device: string | null;
  failReason: string | null;
  loginTime: string;
  user?: {
    id: string;
    firstName?: string;
    lastName?: string;
    email: string;
  };
}

export interface LoginLogStats {
  totalLogins: number;
  successLogins: number;
  failedLogins: number;
  uniqueUsers: number;
  successRate: string;
}

export interface LoginLogListParams {
  page?: number;
  pageSize?: number;
  username?: string;
  status?: 'success' | 'failed';
  ip?: string;
  startTime?: string;
  endTime?: string;
}

/**
 * 获取登录日志列表
 */
export const getLoginLogs = (params: LoginLogListParams) => {
  return get<{ list: LoginLog[]; total: number; page: number; pageSize: number }>(
    '/login-logs',
    params
  );
};

/**
 * 获取登录日志详情
 */
export const getLoginLogById = (id: string) => {
  return get<LoginLog>(`/login-logs/${id}`);
};

/**
 * 获取登录统计
 */
export const getLoginStats = (days: number = 7) => {
  return get<LoginLogStats>('/login-logs/stats', { days });
};

